#!/usr/bin/python 
#-*- coding: UTF-8 -*- 

from django.shortcuts import render_to_response
from django.http import *
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required

#登陆
def player_login(request):
	if request.method == 'GET':
		return render_to_response('login.html')
	else:
		email = request.POST['email']
		password = request.POST['password']
		user = authenticate(username=email, password=password)
		if user is not None:
			login(request, user)
			#next = request.GET('next')
			#if next:
			#	return render_to_response(next)
			return render_to_response('success.html',{'user':user})
		else:
			return render_to_response('fail.html')

#注册
def player_regist(request):
	if request.method == 'GET':
		return render_to_response('register.html')
	else:
		email = request.POST['email']
		password = request.POST['password']
		try:
			user = User.objects.create_user(username=email, email=email, password=password)
		except Exception,data:
			return render_to_response('fail.html',{'error':'用户名已经被注册'})
		if user is not None:
			user.save()
			return render_to_response('success.html')
		else:
			return render_to_response('fail.html')

#注销
def player_logout(request):
	logout(request)
	return render_to_response('index.html')

@login_required
def play(request):
	return render_to_response('index.htm',{'user':request.user})

@login_required
def draw(request):
	return render_to_response('drawTank.htm',{'user':request.user})

@login_required
def main(request):
	return render_to_response('demo.htm',{'user':request.user})